package com.ctv.my.customview.BaiajCustomView.activities;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.ctv.my.customview.BaiajCustomView.View.LoaddingView;
import com.ctv.my.customview.R;

import java.util.Random;

/**
 * 加载Act
 * Created by baiaj on 2017/6/30.
 */

public class LoaddingAct extends AppCompatActivity{

    private static final int REFRESH_PROGRESS = 0x10;

    private LoaddingView loaddingView;
    private int mProgress;

    Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case REFRESH_PROGRESS:
                    if (mProgress == 100){
                        return;
                    }
                    if (mProgress < 40) {
                        mProgress += 1;
                        // 随机800ms以内刷新一次
                        mHandler.sendEmptyMessageDelayed(REFRESH_PROGRESS,
                                new Random().nextInt(600));
                        loaddingView.setProgress(mProgress);
                    } else {
                        mProgress += 1;
                        // 随机1200ms以内刷新一次
                        mHandler.sendEmptyMessageDelayed(REFRESH_PROGRESS,
                                new Random().nextInt(400));
                        loaddingView.setProgress(mProgress);

                    }
                    break;

                default:
                    break;
            }
        };
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loaddingview);
        initView();
        mHandler.sendEmptyMessageDelayed(REFRESH_PROGRESS,2000);
    }

    private void initView() {
        loaddingView = (LoaddingView) findViewById(R.id.loaddingview);
    }

}
